/*
 * 描述:将mdb中的图层别名赋予gdb中相应图层
 * 作者:wx
 * 时间:20160905
 */
package com.ao.action;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import com.esri.arcgis.datasourcesGDB.AccessWorkspaceFactory;
import com.esri.arcgis.datasourcesGDB.FileGDBWorkspaceFactory;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.IClassSchemaEdit;
import com.esri.arcgis.geodatabase.IDataset;
import com.esri.arcgis.geodatabase.IDatasetName;
import com.esri.arcgis.geodatabase.IEnumDataset;
import com.esri.arcgis.geodatabase.IEnumDatasetName;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.geodatabase.esriDatasetType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

public class AddAliasName {

	public static void main(String[] args) throws Exception {
		EngineInitializer.initializeEngine();
		AoInitialize aoInit = new AoInitialize();
	    initializeArcGISLicenses(aoInit);
//		String mdbPath = "D:\\aaa.mdb";
//		String gdbPath = "D:\\bbb.gdb";
//		Map<String, String> map = new HashMap<String, String>();
//		map = getAliasNameArrayFrom(mdbPath);
//		getAliasNameFromMDB(gdbPath,map);
	}

	
	private static void initializeArcGISLicenses(AoInitialize aoInit) {
	    try {
	      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
	          == esriLicenseStatus.esriLicenseAvailable)
	        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
	      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
	          == esriLicenseStatus.esriLicenseAvailable)
	        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
	      else{
	        System.err.println("Could not initialize an Engine or Basic License. Exiting application.");
	        System.exit(-1);
	      }
	    } catch (Exception e) {e.printStackTrace();}
	  }

 
	public static Map<String, String> getAliasNameArrayFrom(String mdbPath) throws UnknownHostException, IOException{
		IWorkspaceFactory MDBworkspaceFactory = new AccessWorkspaceFactory();
		Map<String, String> map = new HashMap<String, String>();
		Workspace MDBworkspace = new Workspace(MDBworkspaceFactory.openFromFile(mdbPath, 0));
		IEnumDataset MDBenumDataset = MDBworkspace.getDatasets(esriDatasetType.esriDTFeatureClass);
		IDataset MDBds = MDBenumDataset.next();
		
		while (MDBds != null) {
			FeatureClass MDBfClass = new FeatureClass(MDBds);
			map.put(MDBfClass.getName(), MDBfClass.getAliasName());
			System.out.println(MDBfClass.getAliasName());
			MDBds = MDBenumDataset.next();
		}
		return map;
	}
	
	public static void getAliasNameFromMDB(String GDBpath,Map<String, String> map) throws Exception{
		// open GDB
		FileGDBWorkspaceFactory GDBfactory = new FileGDBWorkspaceFactory();
		Workspace GDBworkspace = new Workspace(GDBfactory.openFromFile(GDBpath, 0));
		IEnumDataset GDBenumDataset = GDBworkspace.getDatasets(esriDatasetType.esriDTFeatureClass);
		
		IDataset GDBds = GDBenumDataset.next();
		
		while (GDBds != null) {
			FeatureClass GDBfClass = new FeatureClass(GDBds);
			
			if(map.containsKey(GDBfClass.getName())){
				IClassSchemaEdit pClassSchemaEdit = (IClassSchemaEdit)GDBfClass;
				pClassSchemaEdit.alterAliasName(map.get(GDBfClass.getName()));
			}

			System.out.println("FeatureClass NewAliasName: " + GDBfClass.getAliasName());
			GDBds = GDBenumDataset.next();
		}
		
	}

}
